package de.fhb.itunes.controller.web;

import java.io.IOException;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


import de.fhb.itunes.repository.DataAccess.DAO_Film;
import de.fhb.itunes.repository.data.Film_VO;

/**
 * Servlet implementation class index_content
 */
@WebServlet("/index_content")
public class index_content extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public index_content() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String nextJSP = "";
		HttpSession session = request.getSession(false);
		if (session != null) {
			Object objtmp = session.getAttribute("loggedIn");
			boolean isLoggedIn = false;
			if (objtmp != null){
				isLoggedIn = (Boolean) objtmp;
			}
			if (isLoggedIn) {// session.isNew()

			
			Object obj = session.getAttribute("film");
			if (obj instanceof DAO_Film) {
				DAO_Film filme = (DAO_Film) obj;
				List<Film_VO> listofFilms = (List<Film_VO>) filme
						.getLastAdded(5);
				request.setAttribute("lastAdded", listofFilms);

			}
			nextJSP = "/index.jsp";
			}
			else
				nextJSP = "/login.jsp";
			RequestDispatcher dispatcher = getServletContext()
					.getRequestDispatcher(nextJSP);
			dispatcher.forward(request, response);
		} 
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String nextJSP = "";
		HttpSession session = request.getSession(false);
		if (session != null) {
			boolean isLoggedIn = (Boolean) session.getAttribute("loggedIn");
			if (isLoggedIn) {// session.isNew()

			Object obj = session.getAttribute("film");
			if (obj instanceof DAO_Film) {
				DAO_Film filme = (DAO_Film) obj;
				List<Film_VO> listofFilms = (List<Film_VO>) filme
						.getLastAdded(5);
				request.setAttribute("lastAdded", listofFilms);

			}
			nextJSP = "/index.jsp";
			}
			else
				nextJSP = "/login.jsp";
			RequestDispatcher dispatcher = getServletContext()
					.getRequestDispatcher(nextJSP);
			dispatcher.forward(request, response);
		} 
	}

}
